package org.aaron.mp3player.lrc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.Message;

public class LrcProcessor {

	public List<Queue> process(InputStream inputStream) {
		List<Queue> queues = new ArrayList<Queue>();
		// 时间队列
		Queue<Long> timeMills = new LinkedList<Long>();
		// 歌词队列
		Queue<String> lrcs = new LinkedList<String>();
		try {
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "GB2312");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			Pattern pattern = Pattern.compile("\\[([^\\]]+)\\]");
			String temp;
			String result = "";
			while ((temp = bufferedReader.readLine()) != null) {
				Matcher matcher = pattern.matcher(temp);
				if (matcher.find()) {
					String timeStr = matcher.group();
					timeMills.offer(timeToLong(timeStr.substring(1, timeStr.length() - 1)));
					result = "" + temp.substring(10) + "\n";
				} else {
					result += temp + "\n";
				}
				lrcs.add(result);
			}
			queues.add(timeMills);
			queues.add(lrcs);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return queues;
	}

	private Long timeToLong(String timeStr) {
		String[] time1 = timeStr.split(":");
		int min = Integer.parseInt(time1[0]);
		String[] time2 = time1[1].split("\\.");
		int sec = Integer.parseInt(time2[0]);
		int mill = Integer.parseInt(time2[1]);
		return min * 1000 * 60 + sec * 1000 + mill * 10L;
	}
}
